package com.jiankang.askhealth.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import com.jiankang.askhealth.aware.UserAware;
import com.jiankang.askhealth.constant.SessionName;
import com.jiankang.askhealth.core.user.bean.UserBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserInterceptor extends AbstractInterceptor implements StrutsStatics{

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		final Object action = invocation.getAction();
		final ActionContext context = invocation.getInvocationContext(); 

		if(action instanceof UserAware) {
			HttpServletRequest request = (HttpServletRequest)context.get(HTTP_REQUEST);
			UserBean userBean = (UserBean)request.getSession().getAttribute(SessionName.LOGIN_USER);
			((UserAware)action).setLoginUser(userBean);
			
		}
		invocation.getInvocationContext().setLocale(Locale.getDefault());
		return invocation.invoke();
	}

}
